<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /**
     * https://leetcode-cn.com/problems/container-with-most-water/solution/shuang-zhi-zhen-fa-zheng-ming-jian-dan-yi-dong-bu-/
     * 思路：双指针法
     * 一轮比较两个柱子，只移动矮的那支柱子，计算面积 = 矮柱子的高度 * 两柱子距离
     * max记录最大面积
     */ 
    const height = [1,8,6,2,5,4,8,3,7];

    function maxArea(height) {
      let i = 0, j = height.length - 1;
      let area, max = 0;
      
      while (j - i >= 1) {
        if (height[i] > height[j]) {
          area = height[j] * (j - i)
          j--;
        } else {
          area = height[i] * (j - i)
          i++;
        }
        max = Math.max(max, area);
      }
      return max;
    }
    console.log(maxArea(height));
  </script>
</body>
</html>